package org.hk.proxy.server.web.handler;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import io.netty.handler.codec.http.HttpRequest;
import org.hk.proxy.server.db.entity.HkProxySysMenu;
import org.hk.proxy.server.db.mapper.HkProxySysMenuMapper;
import org.hk.proxy.server.utils.SpringUtils;
import org.hk.proxy.server.web.Result;
import org.hk.proxy.server.web.pojo.MenuResp;
import sun.security.krb5.internal.APRep;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class MenuListHandler implements RequestHandler{
    private HkProxySysMenuMapper menuMapper;
    @Override
    public Result request(HttpRequest request) {
        if(menuMapper==null){
            menuMapper = SpringUtils.getBean("hkProxySysMenuMapper",HkProxySysMenuMapper.class);
        }
        List<HkProxySysMenu> menuList = menuMapper.selectList(null);
        if(CollectionUtil.isNotEmpty(menuList)) return Result.success(fillChildrenList(menuList,new ArrayList<>(),new ArrayList<>()));
        return Result.success(null);
    }
    /**
     * 剪枝递归 填充树状菜单结构
     * @param menuList:需要组合的菜单列表
     * @param curParents 当前的父代节点
     * @param rootMenu 根节点
     * @return
     */
    public List<MenuResp> fillChildrenList(List<HkProxySysMenu> menuList, List<MenuResp> curParents,List<MenuResp> rootMenu){
        if(CollectionUtil.isNotEmpty(curParents)){
            if(CollectionUtil.isEmpty(rootMenu)){
                rootMenu.addAll(curParents);
            }
            List<MenuResp> curP = new ArrayList<>();
            List<Integer> parentIds  = curParents.stream().map(MenuResp::getId).collect(Collectors.toList());
            Map<Integer,List<HkProxySysMenu>> childrenGroup = menuList.stream().filter(item->parentIds.contains(item.getParentId())).collect(Collectors.groupingBy(HkProxySysMenu::getParentId));
            if(CollectionUtil.isNotEmpty(childrenGroup)){
                childrenGroup.forEach((k,v)->{
                    MenuResp parent = getById(curParents,k);
                    if(parent!=null){
                        parent.getChildrenList().addAll(v.stream().map(item->convert(item)).collect(Collectors.toList()));
                        curP.addAll(parent.getChildrenList());
                    }
                });
                if(CollectionUtil.isNotEmpty(curP)){
                    fillChildrenList(menuList,curP,rootMenu);
                }
            }
        }else {
            List<HkProxySysMenu> childrens  = menuList.stream().filter(item->item.getParentId()==0).collect(Collectors.toList());
            List<MenuResp>  menuResps = childrens.stream().map(item->convert(item)).collect(Collectors.toList());
            curParents.addAll(menuResps);
            rootMenu.addAll(menuResps);
            List<Integer> pIds = menuResps.stream().map(item->item.getId()).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(pIds)){
                fillChildrenList(menuList,curParents,rootMenu);
            }
        }
        return rootMenu;
    }
    public MenuResp convert(HkProxySysMenu menu){
        MenuResp menuResp = new MenuResp();
        menuResp.setMenuName(menu.getMenuName());
        menuResp.setId(menu.getId());
        menuResp.setParentId(menu.getParentId());
        menuResp.setPath(menu.getPath());
        menuResp.setChildrenList(new ArrayList<>());
        return menuResp;
    }

    private MenuResp getById(List<MenuResp> resps,Integer id){
        for(MenuResp resp : resps){
            if(resp.getId().equals(id)) return resp;
        }
        return null;
    }



}
